package main

import (
	"fmt"
	"reflect"
	"runtime"
	"strconv"
	"time"
)

func main() {
	/*	list := []string{"a", "b", "c", "d"}
		for _, v := range list {
			go testDefer(v)
		}
		for {

		}*/
	f := func(a, b int64) int64 {
		return a + b
	}
	f1 := timedSumFunc(f)
	a := f1(1, 2)
	fmt.Println(a)
}

func testDefer(str string) {
	var Infos []string
	defer func() {
		for _, v := range Infos {
			fmt.Println(v)
		}
	}()
	var i = 0
	for i < 10 {
		i++
		Infos = append(Infos, str+strconv.Itoa(i))
	}
}

func exec() {
	a, b := test(1)
	a()
	b()
}

func test(x int) (func(), func()) {
	return func() {
			fmt.Println(x)
			x = x + 1
		}, func() {
			fmt.Println(x)
		}
}

type SumFunc func(int64, int64) int64

func getFunctionName(i interface{}) string {
	return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

func timedSumFunc(f SumFunc) SumFunc {
	return func(start, end int64) int64 {

		defer func(t time.Time) {
			fmt.Printf("--- Time Elapsed (%s): %v ---\n",
				getFunctionName(f), time.Since(t))
		}(time.Now())

		return f(start, end)
	}
}
